package com.huashi.dealer.modular.controller.api.address;

import com.huashi.dealer.commonEntity.UserAddress;
import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.modular.business.address.param.AddressListsVo;
import com.huashi.dealer.modular.business.address.param.AddressWithRegion;
import com.huashi.dealer.modular.business.address.service.AddressService;
import com.huashi.dealer.modular.business.address.param.AddressResponseParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * API地址控制器
 *
 * @author 谭陈强
 * @since 2025-07-30
 */
@Tag(name = "API地址控制器")
@RestController("apiAddressController")
@RequestMapping("/api/address")
@Validated
public class AddressController {

    @Resource
    private AddressService addressService;

    /**
     * 收货地址列表
     */
    @GetMapping("/lists")
    @Operation(summary = "收货地址列表")
    public CommonResult<AddressListsVo> getAddressList() {
        try {
            AddressListsVo responseParam = addressService.getAddressList();
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取收货地址列表失败：" + e.getMessage());
        }
    }

    /**
     * 添加收货地址
     */
    @PostMapping("/add")
    @Operation(summary = "添加收货地址")
    public CommonResult<String> addAddress(@RequestBody UserAddress requestParam) {
        try {
            addressService.addAddress(requestParam);
            return CommonResult.ok("添加成功");
        } catch (Exception e) {
            return CommonResult.error("添加收货地址失败：" + e.getMessage());
        }
    }

    /**
     * 收货地址详情
     */
    @GetMapping("/detail/{addressId}")
    @Operation(summary = "收货地址详情")
    public CommonResult<AddressWithRegion> getAddressDetail(@PathVariable Long addressId) {
        try {
            AddressWithRegion responseParam = addressService.getAddressDetail(addressId);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取收货地址详情失败：" + e.getMessage());
        }
    }

    /**
     * 编辑收货地址
     */
    @PostMapping("/edit")
    @Operation(summary = "编辑收货地址")
    public CommonResult<String> editAddress(@RequestBody UserAddress requestParam) {
        try {
            addressService.editAddress(requestParam);
            return CommonResult.ok("更新成功");
        } catch (Exception e) {
            return CommonResult.error("编辑收货地址失败：" + e.getMessage());
        }
    }

    /**
     * 设为默认收货地址
     */
    @GetMapping("/setDefault/{addressId}")
    @Operation(summary = "设为默认收货地址")
    public CommonResult<String> setDefaultAddress(@PathVariable Long addressId) {
        try {
            addressService.setDefaultAddress(addressId);
            return CommonResult.ok("设置成功");
        } catch (Exception e) {
            return CommonResult.error("设置默认收货地址失败：" + e.getMessage());
        }
    }

    /**
     * 删除收货地址
     */
    @GetMapping("/delete/{addressId}")
    @Operation(summary = "删除收货地址")
    public CommonResult<String> deleteAddress(@PathVariable Long addressId) {
        try {
            addressService.deleteAddress(addressId);
            return CommonResult.ok("删除成功");
        } catch (Exception e) {
            return CommonResult.error("删除收货地址失败：" + e.getMessage());
        }
    }

    /**
     * 获取省市区列表
     */
    @GetMapping("/region/list")
    @Operation(summary = "获取省市区列表")
    public CommonResult<AddressResponseParam> getRegionList(@RequestParam(required = false) Long parentId) {
        try {
            AddressResponseParam responseParam = addressService.getRegionList(parentId);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("获取省市区列表失败：" + e.getMessage());
        }
    }

    /**
     * 检查收货地址是否存在
     */
    @GetMapping("/check")
    @Operation(summary = "检查收货地址是否存在")
    public CommonResult<AddressResponseParam> checkAddress(@RequestParam String token) {
        try {
            AddressResponseParam responseParam = addressService.checkAddress(token);
            return CommonResult.data(responseParam);
        } catch (Exception e) {
            return CommonResult.error("检查收货地址失败：" + e.getMessage());
        }
    }
} 